﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Xml;
using SMSSnew.smss_web_reference;


namespace SMSSnew.CLASSES
{
    public class classCLZ
    {
        static smss_web_service_app srv = new smss_web_service_app();
        static readXML XMLreader = new readXML();

        public string grade_id;
        public string class_id;
        public string class_teacher_id;

        public classCLZ()
        {
        }


        public classCLZ(string gradeID)
        {
            grade_id = gradeID;
        }
        public classCLZ(string gradeID, string classID, string classTeacherID)
        {
            grade_id = gradeID;
            class_id = classID;
            class_teacher_id = classTeacherID;
        }

        public classCLZ(string classID, string classTeacherID)
        {
            class_id = classID;
            class_teacher_id = classTeacherID;
        }

        public void setClassID(string classID)
        {
            class_id = classID;
        }


        public string addClass()
        {
            string query1 = "", query2 = "";
            string message = "";
            try
            {
                srv = new smss_web_service_app();

                query1 = XMLreader.readXMLtxt("IinsertClass", "~/DATA/XML/classXML.xml");
                query1 = query1.Replace("@class", this.class_id);
                query1 = query1.Replace("@grade", this.grade_id);

                query2 = XMLreader.readXMLtxt("insertClassTeacher", "~/DATA/XML/classXML.xml");
                query2 = query2.Replace("@class", this.class_id);
                query2 = query2.Replace("@classTeacher", this.class_teacher_id);

                if (checkForDuplicateClazEntry(this.class_id) == true)
                {
                    message = "alert('There is already a class defined in same ID');";
                }
                else if (checkForDuplicateClzTeacherEntry(this.class_teacher_id) == true)
                {
                    message = "alert('The teacher is already assigned to a class as  a class teacher');";
                }
                else
                {
                    srv.Modify(query1);
                    srv.Modify(query2);
                    message = "alert('Class is successfully added.');";

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return message;

        }

        public bool checkForDuplicateClazEntry(string clzID)
        {
            string query = "";
            bool havClz = false;
            DataTable dt;
            try
            {

                query = XMLreader.readXMLtxt("checkForDuplicateClazEntry", "~/DATA/XML/classXML.xml");
                query = query.Replace("@class_id", clzID);
                dt = srv.Search(query);

                if (dt.Rows.Count != 0)
                    havClz = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return havClz;
        }

        public bool checkForDuplicateClzTeacherEntry(string clzTeacherID)
        {
            string query = "";
            bool alreadyClzTeacher = false;
            DataTable dt;

            try
            {
                srv = new smss_web_service_app();

                query = XMLreader.readXMLtxt("checkForDuplicateClzTeacherEntry", "~/DATA/XML/classXML.xml");

                query = query.Replace("@class_teacher_id", clzTeacherID);
                dt = srv.Search(query);

                if (dt.Rows.Count != 0)
                    alreadyClzTeacher = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return alreadyClzTeacher;
        }

        //....................method to return no of classes in  particular grade.............
        public int getNoOfClasses(string gradeID)
        {
            string query = "";
            DataTable dt;
            int numberOfClasses;

            try
            {
                srv = new smss_web_service_app();

                query = XMLreader.readXMLtxt("noOfClasses", "~/DATA/XML/classXML.xml");

                query = query.Replace("@grade", gradeID);
                dt = srv.Search(query);

                numberOfClasses = int.Parse(dt.Rows[0][0].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return numberOfClasses;
        }

        //methods for class combination data entry......

        public DataTable loadSubject()
        {
            dt = new DataTable();

            string query = "";


            query = XMLreader.readXMLtxt("load_subject", "~/DATA/XML/classXML.xml");
            dt = srv.Search(query);


            return dt;

        }

        public string insertClassCombination(DataTable dt)  //to insert class combination into database
        {

            try
            {
                DataTable dt1 = new DataTable();
                string query = "";
                string message = "";
                srv = new smss_web_service_app();

                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    if (dt.Rows[i][0].ToString() == "select")
                    {
                        message = "alert('Please select a group for all classes');";
                        return message;
                    }
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    query = XMLreader.readXMLtxt("insertClassCombination", "~/DATA/XML/classXML.xml");

                    // string[] subject = dt.Rows[i][2].ToString().Split('/');

                    query = query.Replace("@group_id", dt.Rows[i][0].ToString());
                    query = query.Replace("@class_id", dt.Rows[i][1].ToString());
                    query = query.Replace("@subject_name", dt.Rows[i][2].ToString());
                    srv.Modify(query);
                }
                message = "alert('Class Combination is successfully added');";
                return message;


            }
            catch
            {
                return ("alert('Error');");
                //throw ex;
            }

        }

        public DataTable select_class_combination(string class_id, string subject_code)               // to get the already assigned group for editing purpose
        {
            string query = "";

            DataTable dt = new DataTable();


            try
            {

                srv = new smss_web_service_app();
                query = XMLreader.readXMLtxt("select_group_id", "~/DATA/XML/classXML.xml");
                query = query.Replace("@class_id", class_id);
                query = query.Replace("@subject_code", subject_code);
                dt = srv.Search(query);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public DataTable load_class_combination(string grade_id, string subject_code)        //to load the information already in the database for the given grade and subject
        {

            string query1 = "";
            DataTable dt = new DataTable();


            try
            {

                srv = new smss_web_service_app();
                query1 = XMLreader.readXMLtxt("load_class_combination", "~/DATA/XML/classXML.xml");

                //    string[] subject = subject_code.Split('/');

                query1 = query1.Replace("@grade_id", grade_id);
                query1 = query1.Replace("@subject_name", subject_code);

                dt = srv.Search(query1);

                return dt;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public string editClassCombination(DataTable dt)            //edit the data in the database
        {
            string query = "";
            string message = "";

            try
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i][1].ToString() == "select")
                    {
                        message = "alert('Please select a group for all classes')";
                        return message;

                    }
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    srv = new smss_web_service_app();
                    query = XMLreader.readXMLtxt("edit_groups", "~/DATA/XML/classXML.xml");
                    //query=xmlr



                    // string[] subject = dt.Rows[i][0].ToString().Split('/');

                    query = query.Replace("@group_id", dt.Rows[i][1].ToString());
                    query = query.Replace("@class_id", dt.Rows[i][2].ToString());
                    query = query.Replace("@subject_name", dt.Rows[i][0].ToString());
                    srv.Modify(query);



                }
                message = "alert('Class is successfully edited')";
                return message;

            }
            catch
            {
                message = "alert('Class is not successfully edited')";
                return message;

            }

        }

        public DataTable add_again(string grade_id, string subject_name)    // to assign group for a new class but already have other classes in that grade in the database
        {
            DataTable dt = new DataTable();

            string query = "";
            srv = new smss_web_service_app();
            //string[] subject = subject_code.Split('/');

            query = XMLreader.readXMLtxt("add_again", "~/DATA/XML/classXML.xml");
            query = query.Replace("@grade_id", grade_id);
            query = query.Replace("@subject_name", subject_name);

            dt = srv.Search(query);
            return dt;

        }

    }
}
